package com.yssoft.controller.app.production;

import com.yssoft.entity.core.TCode;
import com.yssoft.service.production.NonConformityService;
import com.yssoft.utils.AppUtil;

import org.apache.commons.codec.DecoderException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: NonConformityController
 * @类描述: 不合格品管理控制器
 * @作者: 王永宁
 * @创建时间:2018年9月8日09:39:33
 */
@CrossOrigin//解决跨域session问题
@RestController
@RequestMapping("/app/nonConformity")
public class NonConformityController {
    
    @Resource
    private NonConformityService nonConformityService;
    
    /**
     * @方法名称: getNonConformityList
     * @功能描述: 获取不合格品列表
     * @作者: 王永宁
     * @创建时间:2018年9月8日09:41:06
     * @param page 当前页数
     * @param limit 每页条数
     * @param companyId 企业id
     * @param stat 申请状态
     * @return Object
     */
    @RequestMapping(value = "getNonConformityList",method = RequestMethod.GET)
    @ResponseBody
    public Object getNonConformityList(int page, int limit, Integer companyId,String stat) {
    	 if (null == companyId) {
             return AppUtil.returnMapJson("0", "查询失败，请传递企业id", null);
         } 
         try {
             List<Map<String, Object>> nonConformityList = nonConformityService.getNonConformityList(page, limit, companyId,stat);
             return AppUtil.returnListJson("1", "查询成功!", nonConformityList);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }
    
 
	/**
	 * 不合格品处理
     * @作者: 王永宁
     * @创建时间:2018年9月8日13:06:54
	 * @param tInspReport
	 * @param tInspStaEId
	 * @param isUp
	 * @param inspValue
	 * @param request
	 * @param files
	 * @return
	 * @throws IOException
	 * @throws DecoderException
	 */
	@RequestMapping(value = "handle", method = RequestMethod.POST)
    public Object handle(Integer id,Date processingDate,String processingPerson,String processingMethod,BigDecimal processingQuantity, HttpServletRequest request, @RequestParam("files") MultipartFile[] files) throws IOException, DecoderException {
		try {
            return  nonConformityService.handle(id,processingDate,processingPerson,processingMethod,processingQuantity, request, files);
        } catch (Exception e) {
            return AppUtil.returnMapJson("0","保存失败"+e.getMessage(),null);
        }
    }  

    /**
     * @方法名称: getNonConformityDetail
     * @功能描述: 获取不合格品处理记录
     * @作者: 王永宁
     * @创建时间:2018年9月8日14:52:50
     * @param id 
     * @return Object
     */
    @RequestMapping(value = "getNonConformityDetail",method = RequestMethod.POST)
    @ResponseBody
    public Object getNonConformityDetail(Integer id) {
         try {
             Map<String, Object> nonConformityList = nonConformityService.getNonConformityDetail(id);
             return AppUtil.returnMapJson("1", "查询成功!", nonConformityList);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }  
    
    /**
     * 获取商品类型
     * @return
     */
	@RequestMapping(value = "getProductType",method = RequestMethod.POST)
    @ResponseBody
    public Object getProductType() {
         try {
//             List<TCode> codeList = nonConformityService.findProductType();
			 List<Map<String, Object>> productTypeList = new ArrayList<Map<String,Object>>();
//			 for (TCode map : codeList) {
//				Map<String, Object> code = new HashMap<String, Object>();
//				code.put("id", map.getId());
//				code.put("name", map.getDescribed());
//				code.put("classify", map.getClassify());
//				productTypeList.add(code);
//			}
			return AppUtil.returnListJson("1", "查询成功!", productTypeList);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }
}
